package com.xyz.eventoper;

import com.free.common.vo.ResultModel;
import com.xyz.blockqueue.QueueModel;
import com.xyz.dao.logic.CCustomerfundLogic;
import com.xyz.dao.logic.SSettingLogic;
import com.xyz.domain.constants.ConstantsSysSetting;
import com.xyz.domain.ext.EnumEventType;
import com.xyz.domain.ext.EnumFundFlowForm;
import com.xyz.domain.ext.EnumTempCategory;
import com.xyz.domain.table.CCustomer;
import com.xyz.domain.table.CCustomerfriend;
import com.xyz.domain.table.CRecommendhistory;
import com.xyz.domain.table.SSetting;
import com.xyz.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Component
public class RegistEventOperator extends AbstractEventOper{

    @Autowired
    SSettingService settingService;

    @Autowired
    CustomerInfoService customerInfoService;

    @Autowired
    CustomerService customerService;

    @Autowired
    CustomerFundService customerFundService;

    @Autowired
    CustomerFriendService customerFriendService;

    @Autowired
    RecommendHistorySerivice recommendHistorySerivice;

    @Override
    public ResultModel operate(QueueModel model) {

        List<SSetting> settings = settingService.getSettings(ConstantsSysSetting.T_REG);

        // 成功信息
        customerInfoService.insertCustInfoByTmep(model.getCustomerInfo().getId(), EnumTempCategory.REG.toString());

        // 推荐人奖励
        if(model.getCustomerInfo().getRecommender() != null){
            BigDecimal beanAmt = getSettingValueAmt(settings, ConstantsSysSetting.C_REG0030);
            BigDecimal iconAmt = BigDecimal.ZERO;
            BigDecimal diamondAmt = BigDecimal.ZERO;
            customerFundService.addFundInfo(EnumFundFlowForm.ACT_RECOMMEND, model.getCustomerInfo().getRecommender(), beanAmt, iconAmt, diamondAmt);
            addRecommendHistory(model.getCustomerInfo().getRecommender(), model.getCustomerInfo().getId(),1);
            addCustomerFriend(model.getCustomerInfo().getId(), model.getCustomerInfo().getRecommender(), 1);
            //二级推荐奖励
            CCustomer recommender = customerService.getByKey(model.getCustomerInfo().getRecommender());
            if(recommender.getRecommender() != null && recommender.getRecommender() > 0){
                beanAmt = getSettingValueAmt(settings, ConstantsSysSetting.C_REG0031);
                iconAmt = BigDecimal.ZERO;
                diamondAmt = BigDecimal.ZERO;
                customerFundService.addFundInfo(EnumFundFlowForm.ACT_SECRECOMMEND, recommender.getRecommender(), beanAmt, iconAmt, diamondAmt);
                addRecommendHistory(recommender.getRecommender(), model.getCustomerInfo().getId(),2);
                addCustomerFriend(model.getCustomerInfo().getId(), recommender.getRecommender(), 2);
            }
        }

        return ResultModel.success();
    }

    private void addCustomerFriend(Integer customerId, Integer friendId, Integer level){
        CCustomerfriend customerfriend = new CCustomerfriend();
        customerfriend.setCustomerId(customerId);
        customerfriend.setFriendId(friendId);
        customerfriend.setLevel(level);
        customerfriend.setStatus(1);
        Date current = new Date();
        customerfriend.setCreateTime(current);
        customerfriend.setCreateDate(current);
        customerFriendService.addCustFriend(customerfriend);
    }

    private void addRecommendHistory(Integer recommender,Integer register, Integer level){
        CRecommendhistory recommendhistory = new CRecommendhistory();
        recommendhistory.setRegister(register);
        recommendhistory.setRecommender(recommender);
        recommendhistory.setLevel(level);
        recommendhistory.setCreatime(new Date());
        recommendHistorySerivice.addRecommendHistory(recommendhistory);
    }

    private BigDecimal getSettingValueAmt(List<SSetting> settings, String type){
        if(settings == null || StringUtils.isEmpty(type)){
            return BigDecimal.ZERO;
        }
        SSetting settingInfo = settings.stream().filter(x->{
            return x.getCode().equals(type);
        }).findFirst().orElse(null);
        if(settingInfo == null){
            return BigDecimal.ZERO;
        }
        return settingInfo.getAmtValue();
    }


    @Override
    public EnumEventType getEventType() {
        return EnumEventType.RegistEvent;
    }
}
